#include<bits/stdc++.h>
#define maxn 200100

using namespace std;

const double eps=1e-15;

int T;
int n;
int a[maxn];
int color[maxn];
int ans,answ;

inline int calc(){
    int c[maxn]={0};
    for(register int i=1;i<=n;i++){
        bool flag=false;
        for(register int j=i-1;j>=1;j--){
            if(color[i]==color[j]){
                if(a[i]==a[j]) c[i]=a[i];
                break;
            }
        } 
    }
    int res=0;
    for(register int i=1;i<=n;i++) res+=c[i];
    return res;
}

void SA(){
    double t=1,p=0.98;
    while(t>=eps){
        for(register int i=1;i<=n;i++) color[i]=(rand()%2+1);
        int nw=calc();
        int delta=nw-answ;
        if(delta>0) answ=nw,ans=max(ans,answ);
        else if(exp(-delta/t)*RAND_MAX<rand()) answ=nw;
        t*=p;
    }
    for(register int i=1;i<=10;i++){
        for(register int i=1;i<=n;i++) color[i]=(rand()%2+1);
        int nw=calc();
        int delta=nw-answ;
        if(delta>0) answ=nw,ans=max(ans,answ);
    }
}

signed main(){
    freopen("color.in","r",stdin);
    freopen("color.out","w",stdout);
    ios::sync_with_stdio(false);
    cin.tie(0);cout.tie(0);
    srand(time(NULL));
    cin>>T;
    for(register int i=1;i<=T;i++){
        cin>>n;
        for(int i=1;i<=n;i++) cin>>a[i];
        ans=answ=calc();
        SA();SA();SA();
        cout<<ans<<"\n";
    }
    return 0;
}
// ccf can you give me point?I believe you.